<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: HP
 * Date: 2017/6/20
 * Time: 10:20
 */
class Category extends Admin_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->model('category_model');
    }

    public function cat_list(){
        $data['cates'] = $this->category_model->list_cate();
//        var_dump($data['cates']);die;
        $this->load->view('Goods/cat_list.html',$data);
    }
    public function cat_add(){
        if ($_POST){
            $this->form_validation->set_rules('cat_name', '分类名称', 'required');
            if ($this->form_validation->run() == FALSE){
                $data['message'] = validation_errors();
                $data['url'] = site_url('Admin/Category/cat_add');
                $data['wait'] = 3;
                $this->load->view('message.html',$data);
            }else{
                $data['cat_name'] = $this->input->post('cat_name',true);
                $data['parent_id'] = $this->input->post('parent_id');
                $data['unit'] = $this->input->post('unit',true);
                $data['sort_order'] = $this->input->post('sort_order',true);
                $data['is_show'] = $this->input->post('is_show');
                $data['cat_desc'] = $this->input->post('cat_desc',true);
                if ($this->category_model->add_category($data)){
                    unset($data);
                    $data['message'] = "添加分类成功";
                    $data['url'] = site_url('Admin/Category/cat_list');
                    $data['wait'] = 3;
                    $this->load->view('message.html',$data);
                }else{
                    unset($data);
                    $data['message'] = "添加分类失败";
                    $data['url'] = site_url('Admin/Category/cat_add');
                    $data['wait'] = 3;
                    $this->load->view('message.html',$data);
                }
            }
        }else{
            $data['cates'] = $this->category_model->list_cate();
            $this->load->view('Goods/cat_add.html',$data);
        }
    }

    public function cat_edit(){
        if ($_POST){
            $cat_id = $this->input->post('cat_id');
            //获取该类别下的所有子类别
            $sub_cates = $this->category_model->list_cate($cat_id);
            #获取这些后代分类的cat_id
            $sub_ids = array();
            foreach ($sub_cates as $v) {
                $sub_ids[] = $v['cat_id'];
            }
            $parent_id = $this->input->post('parent_id');
            if ($cat_id == $parent_id || in_array($parent_id,$sub_ids)){
                $data['message'] = "不能将分类放置到当前分类或其子分类";
                $data['url'] = site_url('Admin/Category/cat_edit/'.$cat_id);
                $data['wait'] = 3;
                $this->load->view('message.html',$data);
            }else{
                $this->form_validation->set_rules('cat_name', '分类名称', 'required');
                if ($this->form_validation->run() == FALSE){
                    $data['message'] = validation_errors();
                    $data['url'] = site_url('Admin/Category/cat_edit/'.$cat_id);
                    $data['wait'] = 3;
                    $this->load->view('message.html',$data);
                }else{
                    $data['cat_name'] = $this->input->post('cat_name',true);
                    $data['parent_id'] = $this->input->post('parent_id');
                    $data['unit'] = $this->input->post('unit',true);
                    $data['sort_order'] = $this->input->post('sort_order',true);
                    $data['is_show'] = $this->input->post('is_show');
                    $data['cat_desc'] = $this->input->post('cat_desc',true);
                    if ($this->category_model->update_category($data,$cat_id)){
                        unset($data);
                        $data['message'] = "更新分类成功";
                        $data['url'] = site_url('Admin/Category/cat_list');
                        $data['wait'] = 3;
                        $this->load->view('message.html',$data);
                    }else{
                        unset($data);
                        $data['message'] = "更新分类失败";
                        $data['url'] = site_url('Admin/Category/cat_edit/'.$cat_id);
                        $data['wait'] = 3;
                        $this->load->view('message.html',$data);
                    }
                }
            }
        }else{
            $cat_id = $this->uri->segment(4);
            $data['current_cate'] = $this->category_model->get_cate($cat_id);
            $data['cates'] = $this->category_model->list_cate();
            $this->load->view('Goods/cat_edit.html',$data);
        }
    }
}